/**
 * Created by sunny.w <iwisunny@gmail.com> on 15-7-28.
 */
(function(){

    var brand_list=$('#brand_list');
    var shop_list=$('#shop_list');

    wxi.shopBundle.fillSelect(shop_list,[]);

    brand_list.on('change',function(){
        var bid=$(this).val();
        var shop_data=[];
        $.get('/api/get_shop_by_brand',{bid: bid}, function(d){
            if(d.result.length> 0){
                wxi.shopBundle.fillSelect(shop_list, d.result, true);
            }
            else{
                wxi.shopBundle.fillSelect(shop_list, []);
            }
        }, 'json');

        shop_list.val(0).trigger('change');
    });

    if(brand_id){
        brand_list.val(parseInt(brand_id)).trigger('change');
        setTimeout(function(){
            shop_list.val(parseInt(shop_id)).trigger('change');
        }, 1000);
    }

    //购买详情
    wxi.shopBundle.registerEditor($('#buy_detail_editor'), 'buy_detail_editor', function(ue){
        var cont=$('#buy_detail').html();
        ue.setContent(wxi.string.htmlEncode(cont));
    });
    //商品详情
    wxi.shopBundle.registerEditor($('#prod_detail_editor'), 'prod_detail_editor', function(ue){
        var cont=$('#prod_detail').html();
        ue.setContent(wxi.string.htmlEncode(cont));
    });

    //添加商品，表单判断
    $('#btn_submit_form').on('click',function(){
        var fm=$(this).parents('form');

        var entity=wxi.shopBundle.
            preCheckForm(fm, 'shop_adminbundle_product');

        entity.buyDetail=UE.getEditor('buy_detail_editor').getContent();
        entity.detail=UE.getEditor('prod_detail_editor').getContent();

        console.log(entity);

        if(!entity.name){
            wxi.util.tipMsg('', '请填写商品名');
            return false;
        }
        if(entity.brandId == 0){
            wxi.util.tipMsg('', '请选择品牌');
            return false;
        }
        if(entity.shopId == 0){
            wxi.util.tipMsg('', '请选择门店');
            return false;
        }
        if(!entity.description){
            wxi.util.tipMsg('', '请填写商品简介');
            return false;
        }

        if(entity.price==0){
            wxi.util.tipMsg('', '请填写商品价格');
            return false;
        }
        if(entity.originPrice==0){
            wxi.util.tipMsg('', '请填写市场价格');
            return false;
        }

        if(!entity.lifeTime){
            wxi.util.tipMsg('', '请填写商品有效期');
            return false;
        }
        if(!entity.countAll){
            wxi.util.tipMsg('', '请填写商品库存量');
            return false;
        }

        if(!entity.minBuyCount){
            wxi.util.tipMsg('', '请填写最少购买数量');
            return false;
        }
        if(!entity.maxBuyCount){
            wxi.util.tipMsg('', '请填写最大购买数量');
            return false;
        }

        if(!entity.buyDetail){
            wxi.util.tipMsg('', '请填写商品购买须知');
            return false;
        }
        else{
            fm.find('#buy_detail').html(entity.buyDetail);
        }

        if(!entity.detail){
            wxi.util.tipMsg('', '请填写商品详情');
            return false;
        }
        else{
            fm.find('#prod_detail').html(entity.detail);
        }

        fm.submit();


    });


})();